package com.lry.basic.proxy.demo2;

import com.lry.basic.proxy.demo2.anno.Feign;
import com.lry.basic.proxy.demo2.anno.RequestMapping;
import org.springframework.aop.framework.AopProxyUtils;
import org.springframework.util.ClassUtils;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Test {

    public static void main(String[] args) {
        UserService userService = (UserService) Proxy.newProxyInstance(
                UserService.class.getClassLoader(),
                new Class[]{UserService.class},
                new MyInvocationHandler());

        userService.queryUserById(1);


        Object userServices = Proxy.newProxyInstance(
                ClassUtils.getDefaultClassLoader(),
                new Class[]{List.class},
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        List<UserService> list = new ArrayList<>();
                        list.add(new UserServiceImpl());
                        list.add(new UserServiceImpl2());
                        return list;
                    }
                });
        List<UserService> list = (List<UserService>) userServices;
        System.out.println(list);
    }

    static class MyInvocationHandler implements InvocationHandler{



        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            //method.invoke(proxy,args);
//                System.out.println(proxy);
            try{
                Feign feign = method.getDeclaringClass().getAnnotation(Feign.class);
                String serviceName = feign.serviceName();
                RequestMapping requestMapping =  method.getAnnotation(RequestMapping.class);
                String url = requestMapping.url();
                url = serviceName+url;
                System.out.println(url);
                System.out.println(args[0]);
            }catch (Exception e){
                return proxy;
            }
            return null;
        }
    }




}


